package cn.pigeon.health.controller;

import cn.pigeon.health.constant.MessageConstant;
import cn.pigeon.health.constant.RedisKeyConstant;
import cn.pigeon.health.entity.PageResult;
import cn.pigeon.health.entity.QueryPageBean;
import cn.pigeon.health.entity.Result;
import cn.pigeon.health.pojo.CheckGroup;
import cn.pigeon.health.pojo.Setmeal;
import cn.pigeon.health.service.CheckGroupService;
import cn.pigeon.health.service.SetMealService;
import cn.pigeon.health.utils.QiniuTools;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;
    @Reference
    private CheckGroupService checkGroupService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 查找所有的检查组
     * @return
     */
    @RequestMapping("/findAllCheckGroups")
    public Result findAllCheckGroups(){
        try {
            List<CheckGroup> allCheckGroups = checkGroupService.findAllCheckGroups();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,allCheckGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 分页查询套餐
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult setmealList = setMealService.findByPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 添加套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setMealService.add(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 上传图片
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
        try {
            String originalFilename = imgFile.getOriginalFilename();
            int index = originalFilename.lastIndexOf(".");
            String extend = originalFilename.substring((index - 1));

            String rndFileName= UUID.randomUUID().toString()+extend;
            QiniuTools.uploadFile(imgFile.getBytes(),rndFileName);
            jedisPool.getResource().sadd(RedisKeyConstant.SETMEAL_UPLOAD_PIC,rndFileName);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,rndFileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 查询套餐关联的检查组
     */
    @RequestMapping("/findCheckGroupsBySetMealId")
    public Result findCheckGroupsBySetMealId(Integer id){
        try {
            List<Integer> checkGroupIds = setMealService.findCheckGroupsBySetMealId(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkGroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 根据ID查找基本套餐信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setMealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){
        try {
            setMealService.edit(setmeal,checkGroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }


    /**
     * 根据ID删除套餐
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            setMealService.delete(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
}
